<template>
    <!-- 头部搜索框-->
    <el-form :inline="true" :model="formdata" class="demo-form-inline">
        <el-form-item label="类目名称">
            <el-input v-model="formdata.cname" placeholder="请输入类目名称" clearable />
        </el-form-item>
        <el-form-item label="类目编号">
            <el-input v-model="formdata.id" placeholder="请输入编号" clearable />
        </el-form-item>
        <el-form-item label="批量查询">
            <el-input v-model="formdata.ids" placeholder="请输入编号以逗号分隔" clearable />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
    </el-form>
    <!--列表信息-->
    <el-table :data="tableData" style="width: 100%">
        <el-table-column type="selection" width="55" />
        <el-table-column fixed prop="id" label="编号" width="90" />
        <el-table-column prop="cname" label="类目名称" width="100" />
        <el-table-column prop="cimg" label="类目图片" width="120">
            <template #default="scope">
                <img :src="scope.row.cimg" alt="" style="width: 50px;height: 50px;">
            </template>

        </el-table-column>
        <el-table-column prop="seq" label="类目排序" width="90" />
        <el-table-column prop="parentId" label="父目录ID" width="90" />
        <el-table-column prop="status" label="类目状态" width="90">
            <template #default="scope">
                <el-switch v-model="scope.row.childBorder" />
            </template>

        </el-table-column>
        <el-table-column fixed="right" label="操作" min-width="150">
            <template #default="scope">
                <el-button type="danger" size="small" @click="handleClick">
                    下线
                </el-button>
                <el-button type="warning" size="small" @click="EditClick(scope.row.id)">编辑</el-button>
            </template>
        </el-table-column>
    </el-table>
    <el-pagination v-model="pageData" :page-size="pageData.pageSize" :size="size" :background="background"
        layout="total, prev, pager, next" :total="pageData.total" @current-change="handleCurrentChange" />
</template>

<script setup>
import { onMounted, reactive,ref } from 'vue';
// import axios from 'axios';
import { CategoryApi } from '../../api/index.js'
const childBorder = ref(false)
import { useRouter } from 'vue-router';
const router = useRouter();

const formdata = reactive({
    cname: '',
    id: '',
    ids:[]
})
const pageData=reactive({
    pageNum: 1,
    size: 'small',
    pageSize: 3,
    total:10,
    background: true,
})

const tableData =ref( [
    {
        date: '2016-05-03',
        name: 'Tom',
        img: 'https://m.360buyimg.com/seckillcms/jfs/t1/337430/18/7493/54612/68bd4689F2d93cd3b/9c07ba35c4c89db0.jpg',
        city: 'Los Angeles',
        address: 'No. 189, Grove St, Los Angeles',
        zip: 'CA 90036',
        tag: 'Home',
    },
])
onMounted(() => { 
    // axios({
    //     url: 'http://localhost:8080/api/category',
    //     method: 'get'
    // }).then(res => { 
    //     console.log(res);
        
    // })

    CallCategoryApi()
})


const CallCategoryApi = () => { 
    let id = formdata.id == '' ? undefined : formdata.id;
    let ids = formdata.ids == '' ? undefined : formdata.ids;
    let cname = formdata.cname == '' ? undefined : formdata.cname;

    CategoryApi.select.call({
        cname: cname,
        id: id,
        pageNum: pageData.pageNum,
        pageSize:pageData.pageSize
        
    }).then(res => { 
        console.log(res);
        // tableData.value = res
        tableData.value = res.items
        pageData.total = res.total

        res.items.forEach(item => {
            item.childBorder = item.status == 0 ? true:false
        });
       
    })
}

const handleCurrentChange = (num) => {
    
    pageData.pageNum = num
    CallCategoryApi()
   
}

const onSubmit = () => {
    console.log(formdata)
    CallCategoryApi()
}

const EditClick=(id) => { 
console.log(id);
    router.push({ name:'categoryEdit',query:{id:id}})
}
</script>

<style scoped>

</style>